<template>
  <div class="bind-detail padding-big">
    <el-alert type="error">如果您忘记了您的支付密码，可以联系客服进行重置。初始化支付密码时，原支付密码可以忽略。</el-alert>
    <el-form ref="form" :rules="rules" :model="form" label-position="top" label-width="120px" class="padding-big-top">
      <el-form-item label="原支付密码" prop="name">
        <el-input v-model="form.old" placeholder="请输入原支付密码" show-password />
      </el-form-item>

      <el-form-item label="新支付密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入新的支付密码" show-password />
      </el-form-item>

      <el-form-item label="重复支付密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" placeholder="请重复新的支付密码" show-password />
      </el-form-item>
      <!-- 输入短信验证码或者google验证码 -->
      <el-form-item label="验证码" prop="codeStyle">
        <el-radio v-model="form.codeStyle" label="1">谷歌验证器验证</el-radio>
        <!-- <el-radio v-model="form.codeStyle" label="2">短信验证</el-radio> -->
      </el-form-item>

<!--      <el-form-item v-if="form.codeStyle == '2'" prop="smsCode">
        <el-col :xs="12" :sm="12" :md="8" :lg="16" :xl="16">
          <el-input v-model="form.smsCode" placeholder="请点击右侧发送验证码" />
        </el-col>
        <el-col :xs="11" :sm="11" :md="8" :lg="7" :xl="7" class="margin-small-left">
          <el-button type="primary" class="send_btn" :disabled="!send_show" @click="sms_send()">
            <span v-show="send_show">发送验证码</span>
            <span v-show="!send_show">{{ time_wait }}s后可重发</span>
          </el-button>
        </el-col>

        <div class="clearfix" />
        <el-row>
          <el-col><router-link to="/user/bindphone" style="color: #F56C6C">如果您还没有绑定手机号，请点我绑定！</router-link></el-col>
        </el-row>
      </el-form-item> -->

      <el-form-item v-if="form.codeStyle == '1'" prop="googleCode">
        <el-input v-model="form.googleCode" placeholder="请输入谷歌验证器app上的验证码" />
        <el-row>
          <el-col><router-link to="/user/bindgoogle" style="color: #F56C6C">如果您还没有绑定谷歌验证器，请点我绑定！</router-link></el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="submit">立即修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { validatePwd } from '@/utils/validate'
import { setPayPassword } from '@/api/user'
import { sendSms } from '@/api/sms'

export default {
  name: 'UserPayPassword',
  inject: ['reload'],
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!validatePwd(value)) {
        callback(new Error('请填写6-16位字符，不能包含空格'))
      } else {
        callback()
      }
    }
    const validateConfirmPassword = (rule, value, callback) => {
      if (!validatePwd(value)) {
        callback(new Error('请填写6-16位字符，不能包含空格'))
      } else if (value !== this.form.password) {
        callback(new Error('两次密码输入不一致，请检查'))
      } else {
        callback()
      }
    }
    return {
      form: {
        old: '',
        password: '',
        confirmPassword: '',
        codeStyle: '1',
        smsCode: '',
        googleCode: ''
      },
      // 表单验证规则
      rules: {
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: validateConfirmPassword }],
        codeStyle: [{ required: true, tigger: 'blur', message: '请选择验证码类型' }]
      },
      send_show: true,
      time_wait: 60,
      timer: null
    }
  },
  created() {
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 修改支付密码
          setPayPassword(this.form).then((resp) => {
            this.$message({
              message: resp.data.msg,
              type: 'success'
            })
            this.reload()
          }).catch((error) => {
            console.log(error.message)
          })
        }
        return true
      })
    },
    btn_wait() {
      this.$message.success('短信验证码发送成功')
      this.timer = setInterval(() => {
        if (this.time_wait <= 0) {
          this.send_show = true
          this.time_wait = 60
          clearInterval(this.timer)
        } else {
          this.send_show = false
          this.time_wait--
        }
      }, 1000)
    },
    sms_send() {
      sendSms('18888888888', 'changepaypassword').then(resp => {
        this.btn_wait()
      })
    }
  }
}
</script>

<style scoped>

</style>
